home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / domacnost a kancelar / autoit / autoit-v3-setup.exe / Include / File.au3 < prev    next >
Encoding:
Text File  |  2007-09-08  |  26.7 KB  |  701 lines

  1. #include-once
  2.  
  3. ; ------------------------------------------------------------------------------
  4. ;
  5. ; AutoIt Version: 3.0
  6. ; Language:       English
  7. ; Description:    Functions that assist with files and directories.
  8. ;
  9. ; ------------------------------------------------------------------------------
  10.  
  11.  
  12. ;===============================================================================
  13. ;
  14. ; Description:      Returns the number of lines in the specified file.
  15. ; Syntax:           _FileCountLines( $sFilePath )
  16. ; Parameter(s):     $sFilePath - Path and filename of the file to be read
  17. ; Requirement(s):   None
  18. ; Return Value(s):  On Success - Returns number of lines in the file
  19. ;                   On Failure - Returns 0 and sets @error = 1
  20. ; Author(s):        Tylo <tylo at start dot no>
  21. ; Note(s):          It does not count a final @LF as a line.
  22. ;
  23. ;===============================================================================
  24. Func _FileCountLines($sFilePath)
  25.     Local $N = FileGetSize($sFilePath) - 1
  26.     If @error Or $N = -1 Then Return 0
  27.     Return StringLen(StringAddCR(FileRead($sFilePath, $N))) - $N + 1
  28. EndFunc   ;==>_FileCountLines
  29.  
  30.  
  31. ;===============================================================================
  32. ;
  33. ; Description:      Creates or zero's out the length of the file specified.
  34. ; Syntax:           _FileCreate( $sFilePath )
  35. ; Parameter(s):     $sFilePath - Path and filename of the file to be created
  36. ; Requirement(s):   None
  37. ; Return Value(s):  On Success - Returns 1
  38. ;                   On Failure - Returns 0 and sets:
  39. ;                                @error = 1: Error opening specified file
  40. ;                                @error = 2: File could not be written to
  41. ; Author(s):        Brian Keene <brian_keene at yahoo dot com>
  42. ; Note(s):          None
  43. ;
  44. ;===============================================================================
  45. Func _FileCreate($sFilePath)
  46.     ;==============================================
  47.     ; Local Constant/Variable Declaration Section
  48.     ;==============================================
  49.     Local $hOpenFile
  50.     Local $hWriteFile
  51.  
  52.     $hOpenFile = FileOpen($sFilePath, 2)
  53.  
  54.     If $hOpenFile = -1 Then
  55.         SetError(1)
  56.         Return 0
  57.     EndIf
  58.  
  59.     $hWriteFile = FileWrite($hOpenFile, "")
  60.  
  61.     If $hWriteFile = -1 Then
  62.         SetError(2)
  63.         Return 0
  64.     EndIf
  65.  
  66.     FileClose($hOpenFile)
  67.     Return 1
  68. EndFunc   ;==>_FileCreate
  69.  
  70. ;===============================================================================
  71. ;
  72. ; Description:      lists all files and folders in a specified path (Similar to using Dir with the /B Switch)
  73. ; Syntax:           _FileListToArray($sPath, $sFilter = "*", $iFlag = 0)
  74. ; Parameter(s):        $sPath = Path to generate filelist for
  75. ;                   $iFlag = determines weather to return file or folders or both
  76. ;                    $sFilter = The filter to use. Search the Autoit3 manual for the word "WildCards" For details
  77. ;                        $iFlag=0(Default) Return both files and folders
  78. ;                       $iFlag=1 Return files Only
  79. ;                        $iFlag=2 Return Folders Only
  80. ;
  81. ; Requirement(s):   None
  82. ; Return Value(s):  On Success - Returns an array containing the list of files and folders in the specified path
  83. ;                        On Failure - Returns the an empty string "" if no files are found and sets @Error on errors
  84. ;                        @Error=1 Path not found or invalid
  85. ;                        @Error=2 Invalid $sFilter
  86. ;                       @Error=3 Invalid $iFlag
  87. ;                 @Error=4 No File(s) Found
  88. ;
  89. ; Author(s):        SolidSnake <MetalGX91 at GMail dot com>
  90. ; Note(s):            The array returned is one-dimensional and is made up as follows:
  91. ;                    $array[0] = Number of Files\Folders returned
  92. ;                    $array[1] = 1st File\Folder
  93. ;                    $array[2] = 2nd File\Folder
  94. ;                    $array[3] = 3rd File\Folder
  95. ;                    $array[n] = nth File\Folder
  96. ;
  97. ;                    Special Thanks to Helge and Layer for help with the $iFlag update
  98. ;===============================================================================
  99. Func _FileListToArray($sPath, $sFilter = "*", $iFlag = 0)
  100.     Local $hSearch, $sFile, $asFileList[1]
  101.     If Not FileExists($sPath) Then Return SetError(1, 1, "")
  102.     If (StringInStr($sFilter, "\")) Or (StringInStr($sFilter, "/")) Or (StringInStr($sFilter, ":")) Or (StringInStr($sFilter, ">")) Or (StringInStr($sFilter, "<")) Or (StringInStr($sFilter, "|")) Or (StringStripWS($sFilter, 8) = "") Then Return SetError(2, 2, "")
  103.     If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 3, "")
  104.     $hSearch = FileFindFirstFile($sPath & "\" & $sFilter)
  105.     If $hSearch = -1 Then Return SetError(4, 4, "")
  106.     While 1
  107.         $sFile = FileFindNextFile($hSearch)
  108.         If @error Then
  109.             SetError(0)
  110.             ExitLoop
  111.         EndIf
  112.         If $iFlag = 1 And StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") <> 0 Then ContinueLoop
  113.         If $iFlag = 2 And StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") = 0 Then ContinueLoop
  114.         ReDim $asFileList[UBound($asFileList) + 1]
  115.         $asFileList[0] = $asFileList[0] + 1
  116.         $asFileList[UBound($asFileList) - 1] = $sFile
  117.     WEnd
  118.     FileClose($hSearch)
  119.     Return $asFileList
  120. EndFunc   ;==>_FileListToArray
  121.  
  122. ;===============================================================================
  123. ; Function Name:   _FilePrint()
  124. ; Description:     Prints a plain text file.
  125. ; Syntax:          _FilePrint ( $s_File [, $i_Show] )
  126. ;
  127. ; Parameter(s):    $s_File     = The file to print.
  128. ;                  $i_Show     = The state of the window. (default = @SW_HIDE)
  129. ;
  130. ; Requirement(s):  External:   = shell32.dll (it's already in system32).
  131. ;                  Internal:   = None.
  132. ;
  133. ; Return Value(s): On Success: = Returns 1.
  134. ;                  On Failure: = Returns 0 and sets @error according to the global constants list.
  135. ;
  136. ; Author(s):       erifash <erifash [at] gmail [dot] com>
  137. ;
  138. ; Note(s):         Uses the ShellExecute function of shell32.dll.
  139. ;
  140. ; Example(s):
  141. ;   _FilePrint("C:\file.txt")
  142. ;===============================================================================
  143. Func _FilePrint($s_File, $i_Show = @SW_HIDE)
  144.     Local $a_Ret = DllCall("shell32.dll", "long", "ShellExecute", _
  145.             "hwnd", 0, _
  146.             "string", "print", _
  147.             "string", $s_File, _
  148.             "string", "", _
  149.             "string", "", _
  150.             "int", $i_Show)
  151.     If $a_Ret[0] > 32 And Not @error Then
  152.         Return 1
  153.     Else
  154.         SetError($a_Ret[0])
  155.         Return 0
  156.     EndIf
  157. EndFunc   ;==>_FilePrint
  158.  
  159. ;===============================================================================
  160. ;
  161. ; Description:      Reads the specified file into an array.
  162. ; Syntax:           _FileReadToArray( $sFilePath, $aArray )
  163. ; Parameter(s):     $sFilePath - Path and filename of the file to be read
  164. ;                   $aArray    - The array to store the contents of the file
  165. ; Requirement(s):   None
  166. ; Return Value(s):  On Success - Returns 1
  167. ;                   On Failure - Returns 0 and sets @error = 1
  168. ; Author(s):        Jonathan Bennett <jon at hiddensoft dot com>
  169. ; Note(s):          None
  170. ;
  171. ;===============================================================================
  172. Func _FileReadToArray($sFilePath, ByRef $aArray)
  173.     ;==============================================
  174.     ; Local Constant/Variable Declaration Section
  175.     ;==============================================
  176.     Local $hFile
  177.  
  178.     $hFile = FileOpen($sFilePath, 0)
  179.  
  180.     If $hFile = -1 Then
  181.         SetError(1)
  182.         Return 0
  183.     EndIf
  184.  
  185.     $aArray = StringSplit(StringStripCR(FileRead($hFile, FileGetSize($sFilePath))), @LF)
  186.  
  187.     FileClose($hFile)
  188.     Return 1
  189. EndFunc   ;==>_FileReadToArray
  190.  
  191. ;===============================================================================
  192. ;
  193. ; Description:      Write array to File.
  194. ; Syntax:           _FileWriteFromArray( $sFilePath, $aArray )
  195. ; Parameter(s):     $sFilePath - Path and filename of the file to be written
  196. ;                   $a_Array   - The array to retrieve the contents
  197. ;                   $i_Base    - Start reading at this Array entry.
  198. ;                   $I_Ubound  - End reading at this Array entry.
  199. ;                                Default UBound($a_Array) - 1
  200. ; Requirement(s):   None
  201. ; Return Value(s):  On Success - Returns 1
  202. ;                   On Failure - Returns 0 and sets @error = 1
  203. ; Author(s):        Jos van der Zande <jdeb at autoitscript dot com>
  204. ; Note(s):          None
  205. ;
  206. ;===============================================================================
  207. Func _FileWriteFromArray($sFilePath, $a_Array, $i_Base = 0, $i_UBound = 0)
  208.     ;==============================================
  209.     ; Local Constant/Variable Declaration Section
  210.     ;==============================================
  211.     Local $hFile
  212.     ; Check if we have a valid array as input
  213.     If Not IsArray($a_Array) Then
  214.         SetError(2)
  215.         Return 0
  216.     EndIf
  217.     ; determine last entry
  218.     Local $last = UBound($a_Array) - 1
  219.     If $i_UBound < 1 Or $i_UBound > $last Then $i_UBound = $last
  220.     If $i_Base < 0 Or $i_Base > $last Then $i_Base = 0
  221.     ; Open output file
  222.     $hFile = FileOpen($sFilePath, 2)
  223.  
  224.     If $hFile = -1 Then
  225.         SetError(1)
  226.         Return 0
  227.     EndIf
  228.     ;
  229.     FileWrite($hFile, $a_Array[$i_Base])
  230.     For $x = $i_Base + 1 To $i_UBound
  231.         FileWrite($hFile, @CRLF & $a_Array[$x])
  232.     Next
  233.  
  234.     FileClose($hFile)
  235.     Return 1
  236. EndFunc   ;==>_FileWriteFromArray
  237.  
  238. ;===============================================================================
  239. ;
  240. ; Description:      Writes the specified text to a log file.
  241. ; Syntax:           _FileWriteLog( $sLogPath, $sLogMsg )
  242. ; Parameter(s):     $sLogPath - Path and filename to the log file
  243. ;                   $sLogMsg  - Message to be written to the log file
  244. ; Requirement(s):   None
  245. ; Return Value(s):  On Success - Returns 1
  246. ;                   On Failure - Returns 0 and sets:
  247. ;                                @error = 1: Error opening specified file
  248. ;                                @error = 2: File could not be written to
  249. ; Author(s):        Jeremy Landes <jlandes at landeserve dot com>
  250. ; Note(s):          If the text to be appended does NOT end in @CR or @LF then
  251. ;                   a DOS linefeed (@CRLF) will be automatically added.
  252. ;
  253. ;===============================================================================
  254. Func _FileWriteLog($sLogPath, $sLogMsg)
  255.     ;==============================================
  256.     ; Local Constant/Variable Declaration Section
  257.     ;==============================================
  258.     Local $sDateNow
  259.     Local $sTimeNow
  260.     Local $sMsg
  261.     Local $hOpenFile
  262.     Local $hWriteFile
  263.  
  264.     $sDateNow = @YEAR & "-" & @MON & "-" & @MDAY
  265.     $sTimeNow = @HOUR & ":" & @MIN & ":" & @SEC
  266.     $sMsg = $sDateNow & " " & $sTimeNow & " : " & $sLogMsg
  267.  
  268.     $hOpenFile = FileOpen($sLogPath, 1)
  269.  
  270.     If $hOpenFile = -1 Then
  271.         SetError(1)
  272.         Return 0
  273.     EndIf
  274.  
  275.     $hWriteFile = FileWriteLine($hOpenFile, $sMsg)
  276.  
  277.     If $hWriteFile = -1 Then
  278.         SetError(2)
  279.         Return 0
  280.     EndIf
  281.  
  282.     FileClose($hOpenFile)
  283.     Return 1
  284. EndFunc   ;==>_FileWriteLog
  285.  
  286. ;========================================
  287. ;Function name:       _FileWriteToLine
  288. ;Description:         Write text to specified line in a file
  289. ;Parameters:
  290. ;                     $sFile - The file to write to
  291. ;                     $iLine - The line number to write to
  292. ;                     $sText - The text to write
  293. ;                     $fOverWrite - if set to 1 will overwrite the old line
  294. ;                     if set to 0 will not overwrite
  295. ;Requirement(s):      None
  296. ;Return Value(s):     On success - 1
  297. ;                      On Failure - 0 And sets @ERROR
  298. ;                                @ERROR = 1 - File has less lines than $iLine
  299. ;                                @ERROR = 2 - File does not exist
  300. ;                                @ERROR = 3 - Error opening file
  301. ;                                @ERROR = 4 - $iLine is invalid
  302. ;                                @ERROR = 5 - $fOverWrite is invalid
  303. ;                                @ERROR = 6 - $sText is invalid
  304. ;Author(s):           cdkid
  305. ;Note(s):
  306. ;=========================================
  307. Func _FileWriteToLine($sFile, $iLine, $sText, $fOverWrite = 0)
  308.     If $iLine <= 0 Then
  309.         SetError(4)
  310.         Return 0
  311.     EndIf
  312.     If Not IsString($sText) Then
  313.         SetError(6)
  314.         Return 0
  315.     EndIf
  316.     If $fOverWrite <> 0 And $fOverWrite <> 1 Then
  317.         SetError(5)
  318.         Return 0
  319.     EndIf
  320.     If Not FileExists($sFile) Then
  321.         SetError(2)
  322.         Return 0
  323.     EndIf
  324.     Local $filtxt = FileRead($sFile, FileGetSize($sFile))
  325.     $filtxt = StringSplit($filtxt, @CRLF, 1)
  326.     If UBound($filtxt, 1) < $iLine Then
  327.         SetError(1)
  328.         Return 0
  329.     EndIf
  330.     Local $fil = FileOpen($sFile, 2)
  331.     If $fil = -1 Then
  332.         SetError(3)
  333.         Return 0
  334.     EndIf
  335.     For $i = 1 To UBound($filtxt) - 1
  336.         If $i = $iLine Then
  337.             If $fOverWrite = 1 Then
  338.                 If $sText <> '' Then
  339.                     FileWrite($fil, $sText & @CRLF)
  340.                 Else
  341.                     FileWrite($fil, $sText)
  342.                 EndIf
  343.             EndIf
  344.             If $fOverWrite = 0 Then
  345.                 FileWrite($fil, $sText & @CRLF)
  346.                 FileWrite($fil, $filtxt[$i] & @CRLF)
  347.             EndIf
  348.         ElseIf $i < UBound($filtxt, 1) - 1 Then
  349.             FileWrite($fil, $filtxt[$i] & @CRLF)
  350.         ElseIf $i = UBound($filtxt, 1) - 1 Then
  351.             FileWrite($fil, $filtxt[$i])
  352.         EndIf
  353.     Next
  354.     FileClose($fil)
  355.     Return 1
  356. EndFunc   ;==>_FileWriteToLine
  357.  
  358. ;===============================================================================
  359. ;
  360. ; Description:      Treats ..\ as Returns a path after processing the directory change operator .. to move the path up
  361. ;                                one level.
  362. ; Syntax:           _PathFull($sRelativePath, $sBasePath = @WorkingDir)
  363. ; Parameter(s):     $sRelativePath - The path to process.
  364. ;                            $sBasePath$sType - A base path to use if an absolute path is not provided.  This defaults to the
  365. ;                                current working directory.
  366. ; Requirement(s):   None
  367. ; Return Value(s):  The expanded path, the root drive or $sBasePath depending on how it's called.
  368. ; Author(s):        Valik (Original function and modification to rewrite)
  369. ;                        tittoproject (Rewrite)
  370. ; Note(s):          UNC paths are supported.
  371. ;                   Pass "\" to get the root drive of $sBasePath.
  372. ;                   Pass "" or "." to return $sBasePath.
  373. ;                   A relative path will be built relative to $sBasePath.  To bypass this behavior, use an absolute path.
  374. ;
  375. ;===============================================================================
  376. Func _PathFull($sRelativePath, $sBasePath = @WorkingDir)
  377.     If Not $sRelativePath Or $sRelativePath = "." Then Return $sBasePath
  378.  
  379.     ; Normalize slash direction.
  380.     Local $sFullPath = StringReplace($sRelativePath, "/", "\") ; Holds the full path (later, minus the root)
  381.     Local $sPath    ; Holds the root drive/server
  382.     Local $bRootOnly = False    ; Return only root information
  383.  
  384.     ; Check to see if the path is all slashes and if so, set a flag so that
  385.     ; we only return the root path.
  386.     StringReplace($sFullPath, "\", "")
  387.     If @extended = StringLen($sFullPath) Then $bRootOnly = True
  388.  
  389.     ; Check for UNC paths or local drives.  We run this twice at most.  The
  390.     ; first time, we check if the relative path is absolute.  If it's not, then
  391.     ; we use the base path which should be absolute.
  392.     For $i = 1 To 2
  393.         $sPath = StringLeft($sFullPath, 2)
  394.         If $sPath = "\\" Then
  395.             $sFullPath = StringTrimLeft($sFullPath, 2)
  396.             $sPath &= StringLeft($sFullPath, StringInStr($sFullPath, "\") - 1)
  397.             ExitLoop
  398.         ElseIf StringRight($sPath, 1) = ":" Then
  399.             $sFullPath = StringTrimLeft($sFullPath, 2)
  400.             ExitLoop
  401.         Else
  402.             $sFullPath = $sBasePath & "\" & $sFullPath
  403.         EndIf
  404.     Next
  405.  
  406.     ; If this happens, we've found a funky path and don't know what to do
  407.     ; except for get out as fast as possible.  We've also screwed up our
  408.     ; variables so we definitely need to quit.
  409.     If $i = 3 Then Return ""
  410.  
  411.     ; Build an array of the path parts we want to use.
  412.     Local $aTemp = StringSplit($sFullPath, "\")
  413.     Local $aPathParts[$aTemp[0]], $j = 0
  414.     For $i = 2 To $aTemp[0]
  415.         If $aTemp[$i] = ".." Then
  416.             If $j Then $j -= 1
  417.         ElseIf Not ($aTemp[$i] = "" And $i <> $aTemp[0]) And $aTemp[$i] <> "." Then
  418.             $aPathParts[$j] = $aTemp[$i]
  419.             $j += 1
  420.         EndIf
  421.     Next
  422.  
  423.     ; Here we re-build the path from the parts above.  We skip the
  424.     ; loop if we are only returning the root.
  425.     $sFullPath = $sPath
  426.     If Not $bRootOnly Then
  427.         For $i = 0 To $j - 1
  428.             $sFullPath &= "\" & $aPathParts[$i]
  429.         Next
  430.     Else
  431.         $sFullPath &= "\"
  432.     EndIf
  433.  
  434.     ; Clean up the path.
  435.     While StringInStr($sFullPath, ".\")
  436.         $sFullPath = StringReplace($sFullPath, ".\", "\")
  437.     WEnd
  438.     Return $sFullPath
  439. EndFunc   ;==>_PathFull
  440.  
  441. ; ===================================================================
  442. ; Author: Valik
  443. ;
  444. ; _PathMake($szDrive, $szDir, $szFName, $szExt)
  445. ;
  446. ; Creates a string containing the path from drive, directory, file name and file extension parts.  Not all parts must be
  447. ;    passed. The path will still be built with what is passed.  This doesn't check the validity of the path
  448. ;    created, it could contain characters which are invalid on your filesystem.
  449. ; Parameters:
  450. ;     $szDrive - IN - Drive (Can be UNC).  If it's a drive letter, a : is automatically appended
  451. ;     $szDir - IN - Directory.  A trailing slash is added if not found (No preceeding slash is added)
  452. ;     $szFName - IN - The name of the file
  453. ;     $szExt - IN - The file extension.  A period is supplied if not found in the extension
  454. ; Returns:
  455. ;    The string for the newly created path
  456. ; ===================================================================
  457. Func _PathMake($szDrive, $szDir, $szFName, $szExt)
  458.     ; Format $szDrive, if it's not a UNC server name, then just get the drive letter and add a colon
  459.     Local $szFullPath
  460.     ;
  461.     If StringLen($szDrive) Then
  462.         If Not (StringLeft($szDrive, 2) = "\\") Then $szDrive = StringLeft($szDrive, 1) & ":"
  463.     EndIf
  464.  
  465.     ; Format the directory by adding any necessary slashes
  466.     If StringLen($szDir) Then
  467.         If Not (StringRight($szDir, 1) = "\") And Not (StringRight($szDir, 1) = "/") Then $szDir = $szDir & "\"
  468.     EndIf
  469.  
  470.     ; Nothing to be done for the filename
  471.  
  472.     ; Add the period to the extension if necessary
  473.     If StringLen($szExt) Then
  474.         If Not (StringLeft($szExt, 1) = ".") Then $szExt = "." & $szExt
  475.     EndIf
  476.  
  477.     $szFullPath = $szDrive & $szDir & $szFName & $szExt
  478.     Return $szFullPath
  479. EndFunc   ;==>_PathMake
  480.  
  481. ; ===================================================================
  482. ; Author: Valik
  483. ;
  484. ; _PathSplit($szPath, ByRef $szDrive, ByRef $szDir, ByRef $szFName, ByRef $szExt)
  485. ;
  486. ; Splits a path into the drive, directory, file name and file extension parts.  An empty string is set if a
  487. ;    part is missing.
  488. ; Parameters:
  489. ;    $szPath - IN - The path to be split (Can contain a UNC server or drive letter)
  490. ;    $szDrive - OUT - String to hold the drive
  491. ;     $szDir - OUT - String to hold the directory
  492. ;     $szFName - OUT - String to hold the file name
  493. ;     $szExt - OUT - String to hold the file extension
  494. ; Returns:
  495. ;    Array with 5 elements where 0 = original path, 1 = drive, 2 = directory, 3 = filename, 4 = extension
  496. ; ===================================================================
  497. Func _PathSplit($szPath, ByRef $szDrive, ByRef $szDir, ByRef $szFName, ByRef $szExt)
  498.     ; Set local strings to null (We use local strings in case one of the arguments is the same variable)
  499.     Local $drive = ""
  500.     Local $dir = ""
  501.     Local $fname = ""
  502.     Local $ext = ""
  503.     Local $pos
  504.  
  505.     ; Create an array which will be filled and returned later
  506.     Local $array[5]
  507.     $array[0] = $szPath; $szPath can get destroyed, so it needs set now
  508.  
  509.     ; Get drive letter if present (Can be a UNC server)
  510.     If StringMid($szPath, 2, 1) = ":" Then
  511.         $drive = StringLeft($szPath, 2)
  512.         $szPath = StringTrimLeft($szPath, 2)
  513.     ElseIf StringLeft($szPath, 2) = "\\" Then
  514.         $szPath = StringTrimLeft($szPath, 2) ; Trim the \\
  515.         $pos = StringInStr($szPath, "\")
  516.         If $pos = 0 Then $pos = StringInStr($szPath, "/")
  517.         If $pos = 0 Then
  518.             $drive = "\\" & $szPath; Prepend the \\ we stripped earlier
  519.             $szPath = ""; Set to null because the whole path was just the UNC server name
  520.         Else
  521.             $drive = "\\" & StringLeft($szPath, $pos - 1) ; Prepend the \\ we stripped earlier
  522.             $szPath = StringTrimLeft($szPath, $pos - 1)
  523.         EndIf
  524.     EndIf
  525.  
  526.     ; Set the directory and file name if present
  527.     Local $nPosForward = StringInStr($szPath, "/", 0, -1)
  528.     Local $nPosBackward = StringInStr($szPath, "\", 0, -1)
  529.     If $nPosForward >= $nPosBackward Then
  530.         $pos = $nPosForward
  531.     Else
  532.         $pos = $nPosBackward
  533.     EndIf
  534.     $dir = StringLeft($szPath, $pos)
  535.     $fname = StringRight($szPath, StringLen($szPath) - $pos)
  536.  
  537.     ; If $szDir wasn't set, then the whole path must just be a file, so set the filename
  538.     If StringLen($dir) = 0 Then $fname = $szPath
  539.  
  540.     $pos = StringInStr($fname, ".", 0, -1)
  541.     If $pos Then
  542.         $ext = StringRight($fname, StringLen($fname) - ($pos - 1))
  543.         $fname = StringLeft($fname, $pos - 1)
  544.     EndIf
  545.  
  546.     ; Set the strings and array to what we found
  547.     $szDrive = $drive
  548.     $szDir = $dir
  549.     $szFName = $fname
  550.     $szExt = $ext
  551.     $array[1] = $drive
  552.     $array[2] = $dir
  553.     $array[3] = $fname
  554.     $array[4] = $ext
  555.     Return $array
  556. EndFunc   ;==>_PathSplit
  557.  
  558. ; ===================================================================
  559. ; Author: Kurt (aka /dev/null) and JdeB
  560. ;
  561. ; _ReplaceStringInFile($szFileName, $szSearchString, $szReplaceString,$bCaseness = 0, $bOccurance = 0)
  562. ;
  563. ; Replaces a string ($szSearchString) with another string ($szReplaceString) the given TEXT file
  564. ; (via filename)
  565. ;
  566. ; Operation:
  567. ; The funnction opens the original file for reading and a temp file for writing. Then
  568. ; it reads in all lines and searches for the string. If it was found, the original line will be
  569. ; modified and written to the temp file. If the string was not found, the original line will be
  570. ; written to the temp file. At the end the original file will be deleted and the temp file will
  571. ; be renamed.
  572. ;
  573. ; Parameters:
  574. ;     $szFileName         name of the file to open.
  575. ;                ATTENTION !! Needs the FULL path, not just the name returned by eg. FileFindNextFile
  576. ;     $szSearchString        The string we want to replace in the file
  577. ;     $szReplaceString    The string we want as a replacement for $szSearchString
  578. ;     $fCaseness        shall case matter?
  579. ;                0 = NO, case doe not matter (default), 1 = YES, case does matter
  580. ;    $fOccurance        shall we replace the string in every line or just the first occurance
  581. ;                0 = first only, 1 = ALL strings (default)
  582. ;
  583. ; Return Value(s):
  584. ;    On Success         Returns the number of occurences of $szSearchString we found
  585. ;
  586. ;    On Failure         Returns -1 sets @error
  587. ;                    @error=1    Cannot open file
  588. ;                    @error=2    Cannot open temp file
  589. ;                    @error=3    Cannot write to temp file
  590. ;                    @error=4    Cannot delete original file
  591. ;                    @error=5    Cannot rename/move temp file
  592. ;                    @error=6    File has read-only attribute
  593. ;
  594. ; ===================================================================
  595. Func _ReplaceStringInFile($szFileName, $szSearchString, $szReplaceString, $fCaseness = 0, $fOccurance = 1)
  596.  
  597.     Local $iRetVal = 0
  598.     Local $hWriteHandle, $aFileLines, $nCount, $sEndsWith, $hFile
  599.     ; Check if file is readonly ..
  600.     If StringInstr(FileGetAttrib($szFileName),"R") then 
  601.         SetError(6)
  602.         Return -1
  603.     EndIf
  604.     ;===============================================================================
  605.     ;== Read the file into an array
  606.     ;===============================================================================
  607.     $hFile = FileOpen($szFileName, 0)
  608.     If $hFile = -1 Then
  609.         SetError(1)
  610.         Return -1
  611.     EndIf
  612.     Local $s_TotFile = FileRead($hFile, FileGetSize($szFileName))
  613.     If StringRight($s_TotFile, 2) = @CRLF Then
  614.         $sEndsWith = @CRLF
  615.     ElseIf StringRight($s_TotFile, 1) = @CR Then
  616.         $sEndsWith = @CR
  617.     ElseIf StringRight($s_TotFile, 1) = @LF Then
  618.         $sEndsWith = @LF
  619.     Else
  620.         $sEndsWith = ""
  621.     EndIf
  622.     $aFileLines = StringSplit(StringStripCR($s_TotFile), @LF)
  623.     FileClose($hFile)
  624.     ;===============================================================================
  625.     ;== Open the output file in write mode
  626.     ;===============================================================================
  627.     $hWriteHandle = FileOpen($szFileName, 2)
  628.     If $hWriteHandle = -1 Then
  629.         SetError(2)
  630.         Return -1
  631.     EndIf
  632.     ;===============================================================================
  633.     ;== Loop through the array and search for $szSearchString
  634.     ;===============================================================================
  635.     For $nCount = 1 To $aFileLines[0]
  636.         If StringInStr($aFileLines[$nCount], $szSearchString, $fCaseness) Then
  637.             $aFileLines[$nCount] = StringReplace($aFileLines[$nCount], $szSearchString, $szReplaceString, 1 - $fOccurance, $fCaseness)
  638.             $iRetVal = $iRetVal + 1
  639.  
  640.             ;======================================================================
  641.             ;== If we want just the first string replaced, copy the rest of the lines
  642.             ;== and stop
  643.             ;======================================================================
  644.             If $fOccurance = 0 Then
  645.                 $iRetVal = 1
  646.                 ExitLoop
  647.             EndIf
  648.         EndIf
  649.     Next
  650.     ;===============================================================================
  651.     ;== Write the lines back to original file.
  652.     ;===============================================================================
  653.     For $nCount = 1 To $aFileLines[0] - 1
  654.         If FileWriteLine($hWriteHandle, $aFileLines[$nCount]) = 0 Then
  655.             SetError(3)
  656.             FileClose($hWriteHandle)
  657.             Return -1
  658.         EndIf
  659.     Next
  660.     ; Write the last record and ensure it ends with the same as the input file
  661.     If $aFileLines[$nCount] <> "" Then FileWrite($hWriteHandle, $aFileLines[$nCount] & $sEndsWith)
  662.     FileClose($hWriteHandle)
  663.  
  664.     Return $iRetVal
  665. EndFunc   ;==>_ReplaceStringInFile
  666.  
  667. ;========================================================================================================
  668. ;
  669. ; Function Name:    _TempFile([s_DirectoryName],[s_FilePrefix], [s_FileExtension], [i_RandomLength)
  670. ; Description:      Generate a name for a temporary file. The file is guaranteed not to already exist.
  671. ; Parameter(s):
  672. ;     $s_DirectoryName    optional  Name of directory for filename, defaults to @TempDir
  673. ;     $s_FilePrefix       optional  File prefixname, defaults to "~"
  674. ;     $s_FileExtension    optional  File extenstion, defaults to ".tmp"
  675. ;     $i_RandomLength     optional  Number of characters to use to generate a unique name, defaults to 7
  676. ; Requirement(s):   None.
  677. ; Return Value(s):  Filename of a temporary file which does not exist.
  678. ; Author(s):        Dale (Klaatu) Thompson
  679. ;                   Hans Harder - Added Optional parameters
  680. ; Notes:            None.
  681. ;
  682. ;========================================================================================================
  683. Func _TempFile($s_DirectoryName = @TempDir, $s_FilePrefix = "~", $s_FileExtension = ".tmp", $i_RandomLength = 7)
  684.     Local $s_TempName
  685.     ; Check parameters
  686.     If Not FileExists($s_DirectoryName) Then $s_DirectoryName = @TempDir   ; First reset to default temp dir
  687.     If Not FileExists($s_DirectoryName) Then $s_DirectoryName = @ScriptDir ; Still wrong then set to Scriptdir
  688.     ; add trailing \ for directory name
  689.     If StringRight($s_DirectoryName, 1) <> "\" Then $s_DirectoryName = $s_DirectoryName & "\"
  690.     ;
  691.     Do
  692.         $s_TempName = ""
  693.         While StringLen($s_TempName) < $i_RandomLength
  694.             $s_TempName = $s_TempName & Chr(Random(97, 122, 1))
  695.         WEnd
  696.         $s_TempName = $s_DirectoryName & $s_FilePrefix & $s_TempName & $s_FileExtension
  697.     Until Not FileExists($s_TempName)
  698.  
  699.     Return ($s_TempName)
  700. EndFunc   ;==>_TempFile
  701.